package demo;

import java.io.*;
import java.util.Scanner;

public class FileDemo{
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        System.out.println("请输入源文件：");
        String inputFilePath=sc.nextLine();

        System.out.println("请输入目标文件：");
        String outputPath=sc.nextLine();
        File inputFile=new File(inputFilePath);
        if(!inputFile.exists()||!inputFile.isFile()){
            return ;
        }
        File outputFile=new File(outputPath);
        copy(inputFile,outputFile);
    }

    private static void copy(File inputFile,File outputFile){
        //要将源文件的内容复制到目标文件，需要先读取源文件内容，再写入目标文件内容
        try(InputStream inputStream=new FileInputStream(inputFile) ){
            try(OutputStream outputStream=new FileOutputStream(outputFile)){
                byte[]buf=new byte[1024];
                while(true){
                    int len=inputStream.read(buf);
                    if(len==-1){
                        break;
                    }
                    outputStream.write(buf,0,len);
                    outputStream.flush();
                }
            }

        }catch(Exception e){
            e.printStackTrace();
        }
    }




}
